package studio7.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import studio7.exception.DAOException;
import studio7.modelo.Local;
import studio7.util.ConexionBD;

public class LocalesDAO extends BaseDAO{
	

	public Collection<Local> listar() throws DAOException {
			System.out.println("LocalesDAO: listar()");
			Collection<Local> c = new ArrayList<Local>();
			Connection con = null;
			PreparedStatement stmt = null;
			ResultSet rs = null;
			try {
				con = ConexionBD.obtenerConexion();
				String query = "Select idLocal, nombre, direccion  from local";
				stmt = con.prepareStatement(query);
				rs = stmt.executeQuery();
				while (rs.next()) {
					Local vo = new Local();
					vo.setCodigoLocal(rs.getInt("idLocal"));
					vo.setNombreLocal(rs.getString("nombre"));
					vo.setDireccionLocal(rs.getString("direccion"));
				c.add(vo);
		}
			} catch (SQLException e) {
				System.err.println(e.getMessage());
				throw new DAOException(e.getMessage());
			} finally {
				this.cerrarStatement(stmt);
				this.cerrarConexion(con);
			}
			return c;
		}
	
	public int  BuscarLocal(String idLocal) throws DAOException {
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		int nombreLocal=0;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "SELECT nombre  from local where idLocal=?";
			stmt = con.prepareStatement(query);
			stmt.setString(0,idLocal);
			rs = stmt.executeQuery();
			
			while (rs.next()) {
				nombreLocal=rs.getInt(0);
			
				
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOException(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return nombreLocal;
	}
	
	

}
